package com.geek.javase.ch17.work.uploadwork;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/*
 *@Date 2025/11/4
 *@Author wangpeng
 *@Description TODO
 */
public class UploadServer {
    public static void main(String[] args) {
        try(
                //创建服务器
                ServerSocket ss = new ServerSocket(8899);
                //通过ServerSocket读消息
                //接收客户端
                Socket s = ss.accept();
                BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
                //将图片信息挟制服务器磁盘上
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\CodeFile\\igeeklearn\\testJavaSE\\src\\com\\geek\\javase\\ch17\\work\\uploadwork\\newTest.png"));
                //回传消息
                PrintWriter pw = new PrintWriter(s.getOutputStream());
          ){
            System.out.println("UploadServer启动！");
            //上传服务器 边读边写
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
            //写完刷新
            bos.flush();
            //写回上传成功的消息
            pw.println("图片上传成功！");
            pw.flush();

        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
